﻿(function () {
    var DataMorph = window.DataMorph = window.DataMorph || {};

    DataMorph.Api = function(url) {
        var self = this;

        var request = function(verb, method, data, onSuccess, onError) {
            var options = {
                url: url + method,
                type: verb,
                data: data,
                success: function(result, textStatus, jqXhr) {
                    self.apiResult = result;
                    if (onSuccess)
                        onSuccess(result);
                },
                error: function(jqXhr, textStatus, errorText) {
                    var response = null;
                    try {
                        response = $.parseJSON(jqXhr.responseText);
                    } catch(e) {
                        if (onError) {
                            onError(e.toString(), "");
                        }
                    }

                    if (onError) {
                        onError(errorText, response);
                    }
                },
                dataType: "json"
            };

            if (verb == 'POST' || verb == 'PUT') {
                options.data = JSON.stringify(data);
                options.dataType = 'json';
                options.contentType = 'application/json';
            } else {
                options.data = data;
            }

            $.ajax(options);
        };

        var formatError = function(jqXHR, statusText, errorThrown) {
            var statusMessage = (statusText && statusText.ExceptionMessage) ? statusText.ExceptionMessage : statusText;

            return "STATUS: " + statusMessage + " ERROR: " + errorThrown;
        };

        this.GenerateDataSet = function(data, onSuccess, onError) {
            request("PUT", "datagenerator/generatedata", data, onSuccess,
                function(jqXHR, textStatus, errorThrown) {
                    onError(formatError(jqXHR, textStatus, errorThrown));
                }
            );
        };

        this.GetStorageAccounts = function(onSuccess, onError) {
            request("GET", "datagenerator/GetStorageAccounts", null, onSuccess, function(jqXHR, textStatus, errorThrown) {
                onError(formatError(jqXHR, textStatus, errorThrown));
            });
        };

        this.GetDatasets = function(data, onSuccess, onError) {
            request("GET", "datagenerator/GetDataSets", { accountName: data }, onSuccess, function(jqXHR, textStatus, errorThrown) {
                onError(formatError(jqXHR, textStatus, errorThrown));
            });
        };

        this.ProcessDataSet = function(data, onError) {
            request("POST", "datagenerator/ProcessDataSet", data, null, function(jqXHR, textStatus, errorThrown) {
                onError(formatError(jqXHR, textStatus, errorThrown));
            });
        };

        this.GetProcessingTimes = function(storageAccountName, onSuccess, onError) {
            request("GET", "datagenerator/GetProcessingInstances",
                { storageAccount: storageAccountName },
                onSuccess,
                function (jqXHR, textStatus, errorThrown) {
                    onError(formatError(jqXHR, textStatus, errorThrown));
                });
        };
    };
})();